package com.hhq.controller;

import com.hhq.entity.Statistics;
import com.hhq.entity.User;
import com.hhq.service.Userservice;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
    @Autowired
    private Userservice userservice;


    @RequestMapping("/SearchAll")
    public String SearchAll(Model model){
        List<User> list = userservice.getAllUser();
        model.addAttribute("requestScopelist",list);

        for(User user : list)
            System.out.println(user);
        return "user/list";
    }

    @RequestMapping("/findUserByName")
    public String findUserByName(Model model, HttpServletRequest request, String loginname){

        List<User> list = userservice.findUserByName(loginname);
        System.out.println(list);
        if (list.size()!=0){
            model.addAttribute("requestScopelist",list);
            return "user/list";
        }
        else {
            request.setAttribute("message","不存在该用户，请重新输入！");
            return "forward:/SearchAll";
        }
    }
    @RequestMapping("/addUser")
    public String addUser(User user){
        int num = userservice.addUser(user);
        System.out.println(user);
        System.out.println(num);
        return "forward:/SearchAll";
    }

    @RequestMapping("/toAddUser")
    public String toAdd(){
        return "user/add";
    }

    @RequestMapping("/toeditUser")
    public String toeditUser(String loginname,HttpSession session){
        System.out.println(loginname);
        List<User> list = userservice.findUserByName(loginname);
        User user = list.get(0);
        session.setAttribute("user",user);
        return "user/editUser";
    }

    @RequestMapping("/toMyupdate")
    public String toMyupdate(){
        return "user/myupdate";
    }

    @RequestMapping("/deleteUser")
    public String  deleteUser(String loginname){
        userservice.deleteUser(loginname);
        System.out.println("Yes");
        return "forward:/SearchAll";
    }

    @RequestMapping("/updateUser")
    public String updateUser(User user){
        System.out.println(user);
        int num = userservice.updateUser(user);
        System.out.println(num);
        return "forward:/SearchAll";
    }
}
